package com.qianyu.project.web.controller.mfy;

import com.qianyu.project.core.context.user.FlowUserProvider;
import com.qianyu.project.entity.DeviceRequirePlan;
import com.qianyu.project.service.IAttachemenInfoService;
import com.qianyu.project.service.IDeviceRequireListService;
import com.qianyu.project.service.IDeviceRequirePlanService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

/**
 * @Author mfy
 * @desc
 */
@RestController
@RequestMapping("/deviceRequirePlans")
public class deviceRequirePlanController {

    @Autowired
    private IDeviceRequirePlanService deviceRequirePlanService;
    @Autowired
    private FlowUserProvider flowUserProvider;
    @Autowired
    private IDeviceRequireListService deviceRequireListService;
    @Autowired
    private IAttachemenInfoService attachemenInfoService;


    @PostMapping("")
    public Result save(@RequestBody Map<String,Object> params){
        DeviceRequirePlan drp = new DeviceRequirePlan();
        Map<String,Object> mapDrp = (Map<String, Object>) params.get("DeviceRequirePlan");
//        Integer id = deviceRequirePlanService.getAddId(LocalDate.now().toString().split("-")[0]);
//        drp.setId(id);
        drp.setPId(mapDrp.get("pId")+"");
        drp.setPlianName(mapDrp.get("plianName")+"");
        drp.setFillUser(flowUserProvider.getFlowUser().getId().intValue());
        drp.setTbdate(LocalDate.now().toString());
        drp.setXydate(mapDrp.get("xydate")+"");
        drp.setComment(mapDrp.get("comment")+"");
        drp.setApproveStatus(1+"");
        deviceRequirePlanService.Mysave(drp,params);
        return ResultTool.success();
    }

    @PutMapping("")
    public Result update(@RequestBody Map<String,Object> params){
        DeviceRequirePlan drp = new DeviceRequirePlan();
        Map<String,Object> mapDrp = (Map<String, Object>) params.get("DeviceRequirePlan");
        drp.setId(Integer.valueOf(mapDrp.get("id")+""));
        drp.setPId(mapDrp.get("pId")+"");
        drp.setPlianName(mapDrp.get("plianName")+"");
        drp.setFillUser(flowUserProvider.getFlowUser().getId().intValue());
        drp.setTbdate(LocalDate.now().toString());
        drp.setXydate(mapDrp.get("xydate")+"");
        drp.setComment(mapDrp.get("comment")+"");
        drp.setApproveStatus(1+"");
        deviceRequirePlanService.saveOrUpdate(drp);
        deviceRequirePlanService.Myupdate(drp,params);
        return ResultTool.success();
    }


    @GetMapping("")
    public Result list(@RequestParam Map<String,String> params){
        return ResultTool.success(deviceRequirePlanService.Mylist(params));
    }
    @GetMapping("/listNopage")
    public Result listNopage(@RequestParam Map<String,String> params){
        System.out.println(params);
        return ResultTool.success(deviceRequirePlanService.listNopage(params));
    }

    @GetMapping("/selectEnclosures/{attachIds}")
    public Result selectEnclosures(@PathVariable String attachIds){
        return ResultTool.success(attachemenInfoService.selectEnclosures(attachIds));
    }

    @GetMapping("/selectMaterialInfos/{id}")
    public Result selectMaterialInfos(@PathVariable String id){
        return ResultTool.success(deviceRequireListService.selectMaterialInfos(id));
    }
}
